package com.czy.owner.ui.store;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.adapter.OrderProjectAdapter;
import com.czy.owner.api.OrderDetalisApi;
import com.czy.owner.api.OrderSelectSubmitSelectApi;
import com.czy.owner.api.OwnerCarApi;
import com.czy.owner.api.OwnerStoreInfoApi;
import com.czy.owner.api.OwnerStoreStringInfoApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.entity.OrderSelectSubmitMode;
import com.czy.owner.entity.PackageSelectModel;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.entity.SelectOrderDetailsModel;
import com.czy.owner.entity.StoreDetailInfoModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.rxbus.CarEvent;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.garage.MyGarageActivity;
import com.czy.owner.ui.garage.PickBrandActivity;
import com.czy.owner.ui.order.OnLineOrderActivity;
import com.czy.owner.ui.order.SelectBespokeActivity;
import com.czy.owner.ui.workorder.PaidSubscriptionActivity;
import com.czy.owner.ui.workorder.SubscribeOrdelActivity2;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.DialogUtils;
import com.czy.owner.utils.IntentUtils;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.MyRecyclerView;
import com.czy.owner.widget.ScrollEditText;
import com.czy.owner.widget.SelectTimeView;
import com.czy.owner.widget.UniversalDialog;
import com.czy.owner.wxapi.WXPayEntryActivity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by hedepu on 2017-08-23
 */

public class OnLineOrderActivity2 extends BaseActivity {
    @BindView(R.id.img_car_logo)
    ImageView imgCarLogo;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.tv_car_id)
    TextView tvCarId;
    @BindView(R.id.tv_car_brand)
    TextView tvCarBrand;
    @BindView(R.id.tv_select_car)
    TextView tvSelectCar;
    @BindView(R.id.tv_select_time)
    TextView tvSelectTime;

    @BindView(R.id.recyclerview_gv)
    MyRecyclerView recyclerviewGv;
    @BindView(R.id.et_note)
    ScrollEditText etNote;

    @BindView(R.id.tv_package_num)
    TextView tvPackageNum;
    @BindView(R.id.tv_select_card)
    TextView tvSelectCard;
    @BindView(R.id.btn_sure_order)
    Button btnSureOrder;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_str_length)
    TextView tvStrLength;
    @BindView(R.id.line_recyler)
    View lineRecyler;

    private OrderProjectAdapter orderProjectAdapter;
    private List<SelectBespokeListModel> bespokelist = new ArrayList<>();
    private List<PackageSelectModel> selectedList = new ArrayList<>();
    public int serviceNum;
    public static final int REQUEST_DATA = 10001;
    //车库
    public static final int REQUEST_CAR = 10002;
    //新加车辆
    private final static int REQUEST_CODE_ADD_CAR = 1;

    private int currentCarIndex = 0;
    //套餐数
    private String packageNum;
    private CarInfo carInfo;
    private String time;
    private String moneyNum;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_on_line_order_2;
    }

    @Override
    protected void initInjector() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle("");
           /*註冊*/
        RxBus.getDefault().register(this);
        if (getIntent().getSerializableExtra("bespoke") != null)
            bespokelist = (List<SelectBespokeListModel>) getIntent().getSerializableExtra("bespoke");
        if (getIntent().getSerializableExtra("package") != null)
            selectedList = (List<PackageSelectModel>) getIntent().getSerializableExtra("package");
        packageNum = getIntent().getStringExtra("packageNum");
        orderProjectAdapter = new OrderProjectAdapter(this, bespokelist);
        recyclerviewGv.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewGv.setAdapter(orderProjectAdapter);
        recyclerviewGv.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_gray_divider_decoration));
        orderProjectAdapter.setOnDataLine(new OrderProjectAdapter.onDataLine() {
            @Override
            public void onDelModel(SelectBespokeListModel model) {
                bespokelist.remove(model);
            }

            @Override
            public void onSunccess() {
                moneyNum = "0";
                for (SelectBespokeListModel selectBespokeListModel : bespokelist) {
                    moneyNum = DecimalUtil.add(moneyNum + "", DecimalUtil.multiply(selectBespokeListModel.getReservationPrice(), selectBespokeListModel.getCount() + ""));
                }
                btnSureOrder.setText("确认预约，支付订金" + moneyNum + "元");
            }
        });
        initCarInfoData();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            back();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//                startActivity(new Intent(OnLineOrderActivity2.this, SubscribeOrdelActivity2.class).putExtra("currentId", 1));
            back();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void back() {
        new UniversalDialog(this).builder().setTitle("提示")
                .setCancelable(false)
                .setMsg("是否放弃当前预约服务?")
                .setPositiveButton("放弃预约", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setNegativeButton("继续预约", new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        }).show();
    }

    /*接受事件*/
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(CommonEvent changeText) {
        finish();
    }

    /*接受事件*/
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(CarEvent changeText) {
        initCarInfoData();
    }

    @Override
    protected void initViews() {
        imgRight.setImageResource(R.mipmap.order_phone);
        toolbarTitle.setText("在线预约");
        setTextData();
        etNote.addTextChangedListener(new NewWatcher());

    }

    //    验证输入框
    public class NewWatcher implements TextWatcher {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            tvStrLength.setText("剩余" + (30 - etNote.getText().toString().length()) + "个字符");
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
          /*註銷*/
        RxBus.getDefault().unRegister(this);
    }

    //选择数据后重新填数据
    public void setTextData() {
        if (orderProjectAdapter.getList().size() == 0) {
            lineRecyler.setVisibility(View.GONE);
        } else {
            lineRecyler.setVisibility(View.VISIBLE);
        }
        tvPackageNum.setText("(" + packageNum + "套可用）");
        if (selectedList.size() != 0) {
            serviceNum = 0;
            for (PackageSelectModel packageSelectModel : selectedList) {
                serviceNum = serviceNum + packageSelectModel.getPackageItemListBean().getCurrentCount();
            }
            tvSelectCard.setText(serviceNum + "");
            tvSelectCard.setTextColor(this.getResources().getColor(R.color.message_recycler_view_header_text));
            if (serviceNum != 0)
                tvPackageNum.setVisibility(View.GONE);
            else
                tvPackageNum.setVisibility(View.VISIBLE);
        } else {
            tvSelectCard.setText("请选择");
            tvPackageNum.setVisibility(View.VISIBLE);
            tvSelectCard.setTextColor(this.getResources().getColor(R.color.appItemPress));
        }
        moneyNum = "0";
        for (SelectBespokeListModel selectBespokeListModel : bespokelist) {
            moneyNum = DecimalUtil.add(moneyNum + "", DecimalUtil.multiply(selectBespokeListModel.getReservationPrice(), selectBespokeListModel.getCount() + ""));
        }
        btnSureOrder.setText("确认预约，支付订金" + moneyNum + "元");
    }

    @Override
    protected void updateViews() {

    }

    //查询车库
    private void initCarInfoData() {
        OwnerCarApi ownerCarApi = new OwnerCarApi(new HttpOnNextListener<List<CarInfo>>() {
            @Override
            public void onNext(List<CarInfo> o) {
                showCarInfo(o);
            }

            @Override
            public void onCacheNext(String string) {
                super.onCacheNext(string);
                BaseResultEntity<List<CarInfo>> carInfoData = new GsonBuilder().create().fromJson(string, new TypeToken<BaseResultEntity<List<CarInfo>>>() {
                }.getType());
                showCarInfo(carInfoData.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
                Toast.makeText(OnLineOrderActivity2.this, "获取车库信息异常", Toast.LENGTH_SHORT).show();
            }
        }, this);
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("session", UserHelper.getInstance().getSession(this));
        ownerCarApi.setMap(requestMap);
        HttpManager.getInstance().doHttpDeal(ownerCarApi);
    }

    private void showCarInfo(List<CarInfo> list) {
        UserHelper.getInstance().setCarInfoList(list);
        currentCarIndex = UserHelper.getInstance().getDefaultCarIndex();
        if (currentCarIndex != -1) {
            carInfo = list.get(currentCarIndex);
        }
        if (UserHelper.getInstance().getDefaultCarIndex() == -1) {
            new UniversalDialog(this).builder().setTitle("提示")
                    .setCancelable(false)
                    .setMsg("没有发现车辆信息,是否添加车辆")
                    .setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.setClass(OnLineOrderActivity2.this, PickBrandActivity.class);
                            intent.putExtra(PickBrandActivity.START_FLAG, 0);
                            startActivityForResult(intent, REQUEST_CODE_ADD_CAR);
                        }
                    }).setNegativeButton("取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();

                }
            }).show();
        } else {
            //添加数据
            GlideUtils.loadImage(this, carInfo.getImageSrc(), imgCarLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
            tvCarId.setText(carInfo.getLicenseNumber());
            tvCarBrand.setText(carInfo.getChName());
            tvSelectCar.setText("");
        }

    }

    //提交数据
    private void submitData() {
        OrderSelectSubmitSelectApi ownerCarApi = new OrderSelectSubmitSelectApi(new HttpOnNextListener<OrderSelectSubmitMode>() {
            @Override
            public void onNext(OrderSelectSubmitMode o) {
                if (0 == Double.parseDouble(o.getActualDepositAmount())) {
                    getOrderSuccess(o.getUserOrderId());
                } else {
                    Intent ti = new Intent(OnLineOrderActivity2.this, PaidSubscriptionActivity.class);
                    ti.putExtra("orderNumber", o.getOrderNumber());
                    ti.putExtra("userOrderId", o.getUserOrderId());
                    ti.putExtra("type", "1");
                    ti.putExtra("storeId", Integer.parseInt(SharedPreferencesUtils.getParam(OnLineOrderActivity2.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + ""));
                    startActivity(ti);
                    finish();
                }
            }

            @Override
            public void onCacheNext(String string) {
                super.onCacheNext(string);
//                BaseResultEntity<OrderSelectSubmitMode> carInfoData = new GsonBuilder().create().fromJson(string, new TypeToken<BaseResultEntity<OrderSelectSubmitMode>>() {
//                }.getType());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
                Toast.makeText(OnLineOrderActivity2.this, "提交订单信息异常", Toast.LENGTH_SHORT).show();
            }
        }, this);
        String storeServices = "";

        String packageItems = "";
        //选中的门店服务
        for (SelectBespokeListModel selectBespokeListModel : bespokelist) {
            storeServices = storeServices + selectBespokeListModel.getStoreServiceId() + "_" + selectBespokeListModel.getCount() + ",";
        }
        for (PackageSelectModel packageSelectModel : selectedList) {
            if (packageSelectModel.getPackageItemListBean().getCurrentCount() > 0) {
                packageItems = packageItems + packageSelectModel.getPackageItemListBean().getUserPackageItemId() + "_" + packageSelectModel.getPackageItemListBean().getCurrentCount() + ",";
            }
        }
        if (storeServices.length() > 0)
            storeServices = storeServices.substring(0, storeServices.length() - 1);
        if (packageItems.length() > 0)
            packageItems = packageItems.substring(0, packageItems.length() - 1);

        ownerCarApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        ownerCarApi.setStoreId(SharedPreferencesUtils.getParam(OnLineOrderActivity2.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        ownerCarApi.setOwnerCarId(carInfo.getOwnerCarId() + "");
        ownerCarApi.setServiceTime(time);
        ownerCarApi.setStoreServices(storeServices);
        ownerCarApi.setUserPackageItems(packageItems);

        ownerCarApi.setNote(etNote.getText().toString().trim());
        HttpManager.getInstance().doHttpDeal(ownerCarApi);

    }


    //选择时间
    private void showDateDialog(StoreDetailInfoModel storeDetailInfoModel, long serviceTime) {
        final Dialog mDateDialog;

        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = windowManager.getDefaultDisplay();
        mDateDialog = new Dialog(this, R.style.AlertDialogStyle);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_order_add_time, null);
        final SelectTimeView seletc_time_view = (SelectTimeView) dialogView.findViewById(R.id.seletc_time_view);
        seletc_time_view.setData(serviceTime, storeDetailInfoModel.getStoreInfo().getServiceStopTime());

        Button btnCancel = (Button) dialogView.findViewById(R.id.btn_cancel);
        Button btnSure = (Button) dialogView.findViewById(R.id.btn_sure);
        mDateDialog.setContentView(dialogView);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDateDialog.dismiss();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvSelectTime.setText(seletc_time_view.getTimeDay() + seletc_time_view.getTimeHour());
                if ("上午".equals(seletc_time_view.getTimeHour())) {
                    time = TimeUtils.getTimeStamp(seletc_time_view.getTimeDay() + " 09:00", "yyyy年MM月dd日 HH:mm") + "";
//                        PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, seletc_time_view.getTimeDay() + " 09:00");
                } else {
                    time = TimeUtils.getTimeStamp(seletc_time_view.getTimeDay() + " 15:00", "yyyy年MM月dd日 HH:mm") + "";
//                        PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, seletc_time_view.getTimeDay() + " 15:00");
                }
//                    Log.i("-------------",time);

//                    PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, time);
                mDateDialog.dismiss();

            }
        });
        Window dialogWindow = mDateDialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mDisplay.getWidth() * 0.86);
        dialogWindow.setAttributes(p);
        mDateDialog.show();
    }

    //获取详情
    private void getOrderSuccess(String orderId) {
        OrderDetalisApi ownerCarApi = new OrderDetalisApi(new HttpOnNextListener<SelectOrderDetailsModel>() {
            @Override
            public void onNext(SelectOrderDetailsModel o) {
                showDateSuccessDialog(o);
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
                Toast.makeText(OnLineOrderActivity2.this, "获取订单信息异常", Toast.LENGTH_SHORT).show();
            }
        }, this);

        ownerCarApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        ownerCarApi.setStoreId(SharedPreferencesUtils.getParam(OnLineOrderActivity2.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        ownerCarApi.setUserOrderId(orderId);
        HttpManager.getInstance().doHttpDeal(ownerCarApi);
    }

    //成功提示
    private void showDateSuccessDialog(SelectOrderDetailsModel selectOrderDetailsModel) {
        final Dialog mDateSuccessDialog;
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = windowManager.getDefaultDisplay();
        mDateSuccessDialog = new Dialog(this, R.style.AlertDialogStyle);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_order_submit_success, null);
        TextView tv_order_num = (TextView) dialogView.findViewById(R.id.tv_order_num);
        TextView tv_car_num = (TextView) dialogView.findViewById(R.id.tv_car_num);
        TextView tv_time = (TextView) dialogView.findViewById(R.id.tv_time);
        TextView tv_details = (TextView) dialogView.findViewById(R.id.tv_details);
        Button btnSure = (Button) dialogView.findViewById(R.id.btn_sure);
        mDateSuccessDialog.setContentView(dialogView);
        mDateSuccessDialog.setCancelable(false);
        tv_order_num.setText(StringUtils.getNum(selectOrderDetailsModel.getStoreBookingNumber()));
        tv_car_num.setText(selectOrderDetailsModel.getLicenseNumber());
        tv_details.setText(selectOrderDetailsModel.getItemName());
//        String daytime = TimeUtils.timeStamp2Date(selectOrderDetailsModel.getServiceTime(), "HH");
//        String huor;
//        if (Integer.parseInt(daytime) > 12) {
//            huor = "上午";
//        } else {
//            huor = "下午";
//        }
//        tv_time.setText(TimeUtils.timeStamp2Date(selectOrderDetailsModel.getServiceTime(), "yyyy-MM-dd") + " " + TimeUtils.getWeeky(Long.parseLong(selectOrderDetailsModel.getServiceTime()))
//                + huor);
        tv_time.setText(selectOrderDetailsModel.getConvertServiceTime());
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OnLineOrderActivity2.this, SubscribeOrdelActivity2.class).putExtra("currentId", 2));
                mDateSuccessDialog.dismiss();
                finish();

            }
        });

        Window dialogWindow = mDateSuccessDialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mDisplay.getWidth() * 0.86);
        dialogWindow.setAttributes(p);

        mDateSuccessDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_DATA && resultCode == REQUEST_DATA) {
            bespokelist.clear();
            selectedList.clear();
            if (data.getSerializableExtra("bespoke") != null)
                bespokelist.addAll((List<SelectBespokeListModel>) data.getSerializableExtra("bespoke"));
            if (data.getSerializableExtra("package") != null)
                selectedList.addAll((List<PackageSelectModel>) data.getSerializableExtra("package"));
            packageNum = getIntent().getStringExtra("packageNum");
            orderProjectAdapter.addALL(bespokelist);

            setTextData();

        }
        if (resultCode == REQUEST_CAR && requestCode == REQUEST_CAR) {
            carInfo = (CarInfo) data.getSerializableExtra("carInfo");
            //添加数据
            GlideUtils.loadImage(this, carInfo.getImageSrc(), imgCarLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
            tvCarId.setText(carInfo.getLicenseNumber());
            tvCarBrand.setText(carInfo.getChName());
        }
        if (requestCode == REQUEST_CODE_ADD_CAR && resultCode == Constants.ACTIVITY_RESULT_CODE_CAR_ADD) {
            // TODO 车辆添加成功返回
            initCarInfoData();
        }
    }

    //获取门店营业时间  // 或者联系方式
    private void getStoreInfoData(final String typeString) {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_STOREINFO;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(this).getSession());
        params.addBodyParameter("storeId", SharedPreferencesUtils.getParam(OnLineOrderActivity2.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<StoreDetailInfoModel>>() {
                }.getType();
                BaseResultEntity<StoreDetailInfoModel> model = gson.fromJson(result, type);
                if (model.isFlag()) {
                    if ("1".equals(typeString))
                        showDateDialog(model.getData(), model.getServerTime());
                    else if ("2".equals(typeString)) {
                        IntentUtils.onClickCallPhone(OnLineOrderActivity2.this, model.getData().getStoreInfo().getServiceNumber());

                    }
                } else {
                    PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, model.getExp());
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, "获取门店信息失败");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });


//        OwnerStoreStringInfoApi ownerStoreInfoApi = new OwnerStoreStringInfoApi(new HttpOnNextListener<String>() {
//            @Override
//            public void onNext(String model) {
//                PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, "---");
//
////                if ("1".equals(type))
////                    showDateDialog(model);
////                else if ("2".equals(type)) {
////                    IntentUtils.onClickCallPhone(OnLineOrderActivity2.this, model.getStoreInfo().getServiceNumber());
////
////                }
//            }
//
//            @Override
//            public void onCacheNext(String cache) {
//                super.onCacheNext(cache);
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                PhoneUtils.ShowToastMessage(OnLineOrderActivity2.this, e.getMessage());
//            }
//
//        }, this);
////        ownerStoreInfoApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
//        ownerStoreInfoApi.setStoreId(SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
//        HttpManager httpManager = HttpManager.getInstance();
//        httpManager.doHttpDeal(ownerStoreInfoApi);
    }

    @OnClick({R.id.linear_select_time, R.id.linear_select_card, R.id.linear_add_project, R.id.linear_car, R.id.btn_sure_order, R.id.linear_right_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //电话拨打
            case R.id.linear_right_img:
                getStoreInfoData("2");
                break;
            //时间选择
            case R.id.linear_select_time:
                getStoreInfoData("1");
                break;
            //提交数据
            case R.id.btn_sure_order:
                if (carInfo == null) {
                    PhoneUtils.ShowToastMessage(this, "你还未选择车辆");
                    return;
                } else if (StringUtils.isEmpty(time) || Double.parseDouble(time) == 0) {
                    PhoneUtils.ShowToastMessage(this, "你还未添加时间");
                    return;
                } else if (bespokelist.size() == 0 && selectedList.size() == 0) {
                    PhoneUtils.ShowToastMessage(this, "服务和套餐必须选择一种");
                    return;
                }

                submitData();
                break;
            //选择服务
            case R.id.linear_select_card:
                Intent i3 = new Intent(this, SelectBespokeActivity.class);
                i3.putExtra("bespoke", (Serializable) bespokelist)
                        .putExtra("package", (Serializable) selectedList)
                        .putExtra("pos", 1);

                startActivityForResult(i3, REQUEST_DATA);
                break;
            //选择车辆
            case R.id.linear_car:
                if (UserHelper.getInstance().getDefaultCarIndex() == -1) {
                    //添加
                    Intent intent = new Intent();
                    intent.setClass(OnLineOrderActivity2.this, PickBrandActivity.class);
                    intent.putExtra(PickBrandActivity.START_FLAG, 0);
                    startActivityForResult(intent, REQUEST_CODE_ADD_CAR);
                } else {
                    //车库
                    Intent i2 = new Intent(this, MyGarageActivity.class);
                    i2.putExtra("noteditable", true);
                    startActivityForResult(i2, REQUEST_CAR);
                }

                break;
            //
            case R.id.linear_add_project:
                Intent i = new Intent(this, SelectBespokeActivity.class);
                i.putExtra("bespoke", (Serializable) bespokelist)
                        .putExtra("package", (Serializable) selectedList).putExtra("pos", 0);
                ;
                startActivityForResult(i, REQUEST_DATA);
                break;

        }
    }


}
